(function(window){
	var result_container;

	function resultContainer(gameWidth, gameHeight){
		this.gameWidth = gameWidth;
		this.gameHeight	= gameHeight;
		this.background;
		this.congratulationFrame;
		this.answers = new Array();
		this.congratulationFrame;
		this.congratulationText;
		this.level;
		this.ruby;
		this.nextButton;
	}


	resultContainer.prototype = new createjs.Container();
	resultContainer.prototype.Container_initialize = resultContainer.prototype.initialize();

	resultContainer.prototype.initialize = function(){
		console.log("Cai dat container");
		result_container  = this;

		this.background = new createjs.Bitmap(contentmanager.bg_main);
		this.congratulationFrame = new createjs.Bitmap(contentmanager.frame_congratulation);
		this.nextButton = new createjs.Bitmap(contentmanager.btn_continue);

		this.congratulationText = new createjs.Text("","30px Arial","#FFF");

		if (!savescore) savescore = new SaveScore();

		var name = savescore.getUserName();
		if(!name) name = "Bạn";
		this.text1 = new createjs.Text(name + " đã tìm ra đáp án","24px Arial","#FFF");
		this.text2 = new createjs.Text(name + " được tặng thêm 4 Ruby","24px Arial","#FFF");

		this.level= new createjs.Text(qanda.getLevel(),"30px Arial","#000");
		this.ruby = new createjs.Text(savescore.getRuby(), "30px Arial","#000");

		this.configure();
		this.addChild(this.background, this.congratulationFrame, this.congratulationText, this.text1, this.text2, this.nextButton, this.level, this.ruby);
	
	}

	resultContainer.prototype.configure = function(){
		qanda.getLevel();
		savescore.getRuby();
		this.level.text = window.qanda.getLevel();
		this.ruby.text = window.savescore.getRuby();

		this.level.textAlign = "center";
		this.level.x = 540;
		this.level.y = 17;

		this.ruby.textAlign = "center";
		this.ruby.x = 670;
		this.ruby.y = 17;

		this.congratulationFrame.regX = this.congratulationFrame.image.width/2;
		this.congratulationFrame.regY = this.congratulationFrame.image.height/2;
		this.congratulationFrame.x = this.gameWidth/2;
		this.congratulationFrame.y = 140;

		this.congratulationText.x = this.gameWidth/2;
		this.congratulationText.y = 125;
		this.congratulationText.textAlign = "center";

		this.nextButton.regX = this.nextButton.image.width/2;
		this.nextButton.regY = this.nextButton.image.height/2;
		this.nextButton.scaleX = 0.6;
		this.nextButton.scaleY = 0.6;
		this.nextButton.x = this.gameWidth/2;
		this.nextButton.y = 400;


		this.nextButton.addEventListener("mouseover",function(e){
			e.target.scaleX = 0.7;
			e.target.scaleY = 0.7;
		});

		this.nextButton.addEventListener("mouseout", function(e){
			e.target.scaleX = 0.6;
			e.target.scaleY = 0.6;
		});

		this.nextButton.addEventListener("click", function(e){
			soundClick();
			createjs.Sound.stop();
			e.target.scaleX = 0.6;
			e.target.scaleX = 0.6;
			ingame.visible = true;
			ingame.alpha = 1;
			createjs.Tween.get(result_container).to({alpha:0},1000).call(function(){
				result_container.visible = false;
			});
			
		});

		this.text1.textAlign = "center";
		this.text2.textAlign = "center";
		this.text1.x = this.gameWidth/2;
		this.text2.x = this.gameWidth/2;

		this.text1.y = 200;
		this.text2.y = 320;
	}

	resultContainer.prototype.generateAnswer = function(){
		playingSound = soundBravo();
		//Cap nhat score, Level 
		savescore.update();
	
	
		this.visible = true;

		this.removeAns();
		tempAns = qanda.getAnswer();
		tempAns.toUpperCase();

		for( i = 0; i < tempAns.length; i++){
			this.answers[i] = new Array();
			this.answers[i][0] = new createjs.Bitmap(contentmanager.item_answer);
			this.answers[i][1] = new createjs.Text(tempAns[i].toUpperCase(),"28px Arial","#0F0");

			this.answers[i][0].regX = this.answers[i][0].image.width/2;
			this.answers[i][0].regY = this.answers[i][0].image.height/2; 
			this.answers[i][0].y = 270;
			this.answers[i][0].scaleX = 0.8;
			this.answers[i][0].scaleY = 0.8;
			this.answers[i][0].x = this.gameWidth/2 + this.answers[i][0].image.width*0.9*(0.5 + i-tempAns.length/2);
			this.answers[i][1].textAlign = "center";
			this.answers[i][1].x = this.answers[i][0].x;
			this.answers[i][1].y = 255;
			if(tempAns[i] == " "){
				this.answers[i][0].alpha = 0;
				this.answers[i][1].alpha = 0;
			}
			this.addChild(this.answers[i][0], this.answers[i][1]);
		}

		var congraText = new Array("Tuyệt vời!","Bạn thật xuất sắc!","Thiên tài!","Bạn thông minh vãi!!!","Chính cmn xác");
		var rand = Math.floor(Math.random()*5);

		this.congratulationText.text = congraText[rand].toUpperCase();
	}

	resultContainer.prototype.removeAns = function(){
		// bỏ hết mấy thằng con cho lượt hiện tại, chuẩn bị cho lứa mới
		if(this.answers.length > 1)
			for(i = 0; i < this.answers.length; i++){
				this.removeChild(this.answers[i][0]);
				this.removeChild(this.answers[i][1]);
			}
		else
			return;
	}

	window.resultContainer = resultContainer;

}(window));